﻿using System;
using System.Text;

namespace T4UtilityBelt.UxDesigns.Styles
{
    public struct Color : IFormattable
    {
        private MilColor _sRgbColor;

        public byte A
        {
            get { return _sRgbColor.a; }
            set { _sRgbColor.a = value; }
        }

        public byte B
        {
            get { return _sRgbColor.b; }
            set { _sRgbColor.b = value; }
        }

        public byte G
        {
            get { return _sRgbColor.g; }
            set { _sRgbColor.g = value; }
        }

        public byte R
        {
            get { return _sRgbColor.r; }
            set { _sRgbColor.r = value; }
        }

        string IFormattable.ToString(string format, IFormatProvider provider)
        {
            return ConvertToString(format, provider);
        }

        internal string ConvertToString(string format, IFormatProvider provider)
        {
            var stringBuilder = new StringBuilder();
            if (format != null)
            {
                //char numericListSeparator = TokenizerHelper.GetNumericListSeparator(provider);
                var strArrays = new string[9];
                strArrays[0] = "sc#{1:";
                strArrays[1] = format;
                strArrays[2] = "}{0} {2:";
                strArrays[3] = format;
                strArrays[4] = "}{0} {3:";
                strArrays[5] = format;
                strArrays[6] = "}{0} {4:";
                strArrays[7] = format;
                strArrays[8] = "}";
                var objArray = new object[5];
                objArray[0] = ".";
                objArray[1] = _sRgbColor.a;
                objArray[2] = _sRgbColor.r;
                objArray[3] = _sRgbColor.g;
                objArray[4] = _sRgbColor.b;
                stringBuilder.AppendFormat(provider, string.Concat(strArrays), objArray);
            }
            else
            {
                var objArray1 = new object[1];
                objArray1[0] = _sRgbColor.a;
                stringBuilder.AppendFormat(provider, "#{0:X2}", objArray1);
                var objArray2 = new object[1];
                objArray2[0] = _sRgbColor.r;
                stringBuilder.AppendFormat(provider, "{0:X2}", objArray2);
                var objArray3 = new object[1];
                objArray3[0] = _sRgbColor.g;
                stringBuilder.AppendFormat(provider, "{0:X2}", objArray3);
                var objArray4 = new object[1];
                objArray4[0] = _sRgbColor.b;
                stringBuilder.AppendFormat(provider, "{0:X2}", objArray4);
            }
            return stringBuilder.ToString();
        }

        //internal static Color Create(object o)
        //{
        //    if (o == null)
        //    {
        //        var color = new Color();
        //        return color;
        //    }
        //    else
        //    {
        //        return FromUInt32((uint) o);
        //    }
        //}

        public override bool Equals(object obj)
        {
            if (!(obj is Color))
            {
                return false;
            }
            else
            {
                var color = (Color) obj;
                return this == color;
            }
        }

        public bool Equals(Color color)
        {
            return this == color;
        }

        public static Color FromArgb(byte a, byte r, byte g, byte b)
        {
            var color = new Color {_sRgbColor = {a = a, r = r, g = g, b = b}};
            return color;
        }

        //internal static Color FromUInt32(uint argb)
        //{
        //    var color = new Color
        //        {
        //            _sRgbColor =
        //                {
        //                    a = (byte) ((argb & -16777216) >> 24),
        //                    r = (byte) ((argb & 16711680) >> 16),
        //                    g = (byte) ((argb & 65280) >> 8),
        //                    b = (byte) (argb & 255)
        //                }
        //        };
        //    return color;
        //}

        public override int GetHashCode()
        {
            return _sRgbColor.GetHashCode();
        }

        public static bool operator ==(Color color1, Color color2)
        {
            if (color1.R == color2.R)
            {
                if (color1.G == color2.G)
                {
                    if (color1.B == color2.B)
                    {
                        if (color1.A == color2.A)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public static bool operator !=(Color color1, Color color2)
        {
            return !(color1 == color2);
        }

        public override string ToString()
        {
            return ConvertToString(null, null);
        }

        public string ToString(IFormatProvider provider)
        {
            return ConvertToString(null, provider);
        }

        //internal uint ToUInt32()
        //{
        //    uint num = this.sRgbColor.a << 24 | this.sRgbColor.r << 16 | this.sRgbColor.g << 8 | this.sRgbColor.b;
        //    return num;
        //}

        private struct MilColor
        {
            public byte a;

            public byte b;
            public byte g;
            public byte r;
        }
    }
}